package com.tron.game;

import com.tron.game.configs.BscConfig;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;

import java.math.BigInteger;
import java.util.concurrent.TimeUnit;

/**
 * 初始化类
 */
@Order(1) // @Order注解可以改变执行顺序，越小越先执行
@Component
public class MyApplicationRunner1 implements ApplicationRunner {
    @Autowired
     BscConfig tronConfig;
    /**
     * 会在服务启动完成后立即执行
     */
    @Override
    public void run(ApplicationArguments arg0) throws Exception {

        String url3 = "https://bsc-dataseed.binance.org";



        if("test".equals(tronConfig.getNetwork())) {
            url3 = "https://data-seed-prebsc-1-s1.binance.org:8545";
        }

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(30*1000, TimeUnit.MILLISECONDS);
        builder.writeTimeout(30*1000, TimeUnit.MILLISECONDS);
        builder.readTimeout(30*1000, TimeUnit.MILLISECONDS);
        OkHttpClient httpClient = builder.build();
        Web3j web3j = Web3j.build(new HttpService(url3,httpClient,false));
        //转账的凭证，需要传入私钥
        Credentials credentials = Credentials.create( tronConfig.getPrivateKey());
        //Web3j web3j = Web3j.build(new HttpService(url3));
        ContractGasProvider contractGasProvider = new DefaultGasProvider();

       // String hexPrivateKey = String.format("%040x", new BigInteger(1, tronConfig.getPrivateKey().getBytes()));
        //String hexPublicKey = String.format("%040x", new BigInteger(1, tronConfig.getPrivateKey().getBytes()));
       // Credentials creds = Credentials.create(hexPrivateKey, hexPublicKey);
        Lira lira=LiraHelper.Init(tronConfig.getContract_address(),web3j,credentials,BigInteger.valueOf(27000000000L), BigInteger.valueOf(250000));

        System.out.println("MyApplicationRunner1----" + arg0);
    }

}